<template>
  <div class="the-page">

    <div class="panel-table">
      <div class="table-head">
        <span>页面配置</span>
        <div>
          <el-button type="primary" icon="icon-plus" @click.stop="createPageHandle()">新增页面</el-button>
        </div>
      </div>
      <!-- <el-table :data="list" border fit highlight-current-row style="width: 100%"> -->
      <el-table :data="list" stripe :header-cell-style="{backgroundColor:'#ededed',padding:'12px 0'}" style="width: 100%;" border>
        <el-table-column align="center" label="编号" width="120" prop="id" />

        <el-table-column align="center" label="页面名称" min-width="120" prop="name">
          <template #default="{row}">
            <router-link :to="`/config/list/blocks/${row.id}` " class="link-type">
              <span>{{ row.name }}</span>
            </router-link>
          </template>
        </el-table-column>

        <el-table-column align="center" label="别名" min-width="120" prop="alias" />

        <el-table-column align="center" label="创建时间" min-width="160" prop="createdAt" />

        <el-table-column label="操作" fixed="right" align="center" width="220px">
          <template #default="{row}">
            <el-button @click="$router.push(`/config/list/blocks/${row.id}`)" type="primary" size="small">区块管理</el-button>
            <el-button @click="editPageHandle(row)" type="primary" plain size="small">编辑</el-button>
            <el-button @click="deletePageHandle(row)" type="danger" plain size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="table-pagination">
        <pagination :total="total" v-model:page="listQuery.page" v-model:limit="listQuery.pageSize" @pagination="getList" />
      </div>

    </div>

    <el-dialog :title="pageForm.id?'编辑页面':'新增页面'" v-model="pageMadalShow">
      <el-form ref="pageForm" :model="pageForm" :rules="pageFormRules" label-width="100px">
        <el-form-item label="页面名称" prop="name">
          <el-input v-model="pageForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="别名" prop="alias">
          <el-input v-model="pageForm.alias" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="pageMadalShow = false">取 消</el-button>
          <el-button type="primary" @click="confirmHandle()">确 定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import { getPageList, createPage, editPage, deletePage } from "@/api/config";
import { ElNotification, ElMessageBox } from "element-plus";
export default {
  name: "ConfigList",
  components: { Pagination },
  data() {
    return {
      list: [],
      listQuery: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      pageMadalShow: false,
      pageForm: {
        name: "",
        alias: "",
      },
      pageFormRules: {
        name: [{ required: true, message: "请输入页面名称", trigger: "blur" }],
        alias: [{ required: true, message: "请输入别名", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      getPageList(this.listQuery).then((res) => {
        this.list = res.data.items;
        this.total = res.data.pagination.total_record;
      });
    },
    createPageHandle() {
      this.pageMadalShow = true;
      this.$nextTick(() => {
        this.$refs["pageForm"].resetFields();
        this.pageForm.id = null;
      });
    },
    editPageHandle(item) {
      this.pageMadalShow = true;
      this.$nextTick(() => {
        this.$refs["pageForm"].resetFields();
        this.pageForm.name = item.name;
        this.pageForm.alias = item.alias;
        this.pageForm.id = item.id;
      });
    },
    confirmHandle() {
      this.$refs["pageForm"].validate((valid) => {
        if (valid) {
          if (this.pageForm.id) {
            editPage(this.pageForm).then(() => {
              this.pageMadalShow = false;
              this.getList();
            });
          } else {
            createPage(this.pageForm).then(() => {
              this.pageMadalShow = false;
              this.getList();
            });
          }
        }
      });
    },
    deletePageHandle(item) {
      ElMessageBox.confirm("是否删除该页面配置?", "提示", {
        type: "warning",
      }).then(() => {
        deletePage(item.id).then(() => {
          ElNotification({
            title: "提示",
            message: "删除成功",
            type: "success",
            duration: 2000,
          });
          this.getList();
        });
      });
    },
  },
};
</script>

<style scoped lang="scss">
@import "@/styles/tablePage.scss";
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
